import os,sys
sys.path.append(os.path.dirname(sys.path[0]))
# from curd.cql2sql import CqlFilter2Sql
# from curd.Filter2SQL import Filter2SqlWhere
# from pydantic import BaseModel
# from fastapi import FastAPI
# import uvicorn

# app=FastAPI(docs_url="/ss_docs")
# class st(BaseModel):
#     sts:str=None
#     df:str=None

# @app.post("/")
# def sd(cv:st):
#     if cv.sts:
#          print(123)


import json
import gpudb
from shapely.geometry import Polygon
from setting.config import dbhost,dbport,dbaccount,dbpassword
import requests

kdb=gpudb.GPUdb(host = f"http://{dbhost}:{dbport}", username = dbaccount, password = dbpassword)



def execute(sql):

    response=kdb.execute_sql(statement=sql,limit=-9999,encoding="json",request_schema_str="",data=[])
 
    print(response)
    # query_dict=json.loads(response["json_encoded_response"])
    
    # return query_dict
    



if __name__=="__main__":
#    create_view_sql = """
# CREATE VIEW my_view AS
# SELECT * FROM my_table
# WHERE condition_column > 10;
# """
   sql=" CREATE VIEW ki_stac.views AS select * from ki_stac.daily_data_test e  join ki_home.daily_data_view m on e.SlID=m.SlID and e.ImgGSD=m.ImgGSD where (datetime >= '2023-02-01T16:00:00.000Z') AND (datetime <= '2024-02-15T15:59:59.000Z') AND (Cloud <= 50) AND (dataset_id IN (83, 185, 186, 47, 50)) and ST_INTERSECTS(geometry,ST_SIMPLIFYPRESERVETOPOLOGY(('POLYGON ((106.74606622688269 37.35174809236082, 106.7460662268827 31.03009920474597, 112.66246028358103 31.03009920474597, 112.66246028358103 37.35174809236082, 106.74606622688269 37.35174809236082))'),0.01)) order by  datetime desc limit 10 "
   execute(sql) 











# query3 = {
#     "op": "and",
#     "args": [
#         {"op": "=", "args": [{"property": "age"}, 18]},
#         {"op": "<", "args": [{"property": "datetime"}, "2024-01-01"]},
#         {"op":"or","args":[{"op": "=", "args": [{"property": "sd"}, 18]},
#         {"op": "<", "args": [{"property": "fg"}, "2024-01-01"]}]},
#         {"op":"in","args":[{"property":"cvs"},[1,5,8]]},
#         {"op":"isnull","args":[{"property":"df"}]},
#         {"op":"between","args":[{"property":"fn"},[1,8]]},
#         {
#     "op": "s_intersects",
#     "args": [
#       { "property": "geometry" } ,
#       {
#         "type": "Polygon",
#         "coordinates": [[
#             [-77.0824, 38.7886], [-77.0189, 38.7886],
#             [-77.0189, 38.8351], [-77.0824, 38.8351],
#             [-77.0824, 38.7886]
#         ]]
#       }
#     ]
#   },
#   {
#     "op": "t_intersects",
#     "args": [
#       { "property": "datetime" },
#       { "interval" : [ "..", "2020-11-12T00:00:00Z"] }
#     ]
#   }
#     ]
# }


# # s=Filter2SqlWhere(query3).query_to_sql()
# # print(s)